package com.tsj.spzx.controller;

import com.tsj.spzx.model.entity.product.Category;
import com.tsj.spzx.model.vo.common.Result;
import com.tsj.spzx.model.vo.common.ResultCodeEnum;
import com.tsj.spzx.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
@RestController
@RequestMapping("/admin/product/category")
@Tag("分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父菜单的id查询菜单用来展示，一次只查一层
     * @param parentId 根菜单就是0
     * @return
     */
    @Operation(summary = "根据parentId获取下级节点")
    @GetMapping("/findByParentId/{parentId}")
    public Result findByParentId(@PathVariable("parentId") Long parentId){
        List<Category> categoryList = categoryService.findByParentId(parentId);
        return Result.build(categoryList, ResultCodeEnum.SUCCESS);
    }

    /**
     * 导出数据
     * @param httpServletResponse
     */
    @Operation(summary = "导出数据")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse httpServletResponse){
        // 需要在响应头中添加相应信息
        categoryService.exportData(httpServletResponse);
    }


    /**
     * 导入数据
     * @return
     */
    @Operation(summary = "导入数据")
    @PostMapping("/importData")
    public Result importData(MultipartFile file){
        categoryService.importData(file);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
